package com.gz.purchase.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gz.purchase.R;
import com.gz.purchase.library.pojo.ProductInfo;
import com.gz.purchase.library.pojo.PurchaseOrderInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * author wanner1991
 */
public class PurcahseOrderAdapter extends BaseAdapter {
    private Context mContext;
    private List<PurchaseOrderInfo> mDataList;
    private DeleteOrderListener deleteOrderListener;

    public PurcahseOrderAdapter(Context mContext, List<PurchaseOrderInfo> mDataList, DeleteOrderListener deleteOrderListener) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        this.deleteOrderListener = deleteOrderListener;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder= null;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.purchase_order_item,null);
            holder = new ViewHolder();
            holder.tv_order_number = (TextView) view.findViewById(R.id.tv_order_number);
            holder.tv_create_time = (TextView) view.findViewById(R.id.tv_create_time);
            holder.tv_should_pay = (TextView) view.findViewById(R.id.tv_should_pay);
            holder.tv_real_pay = (TextView) view.findViewById(R.id.tv_real_pay);
            holder.tv_notes = (TextView) view.findViewById(R.id.tv_notes);
            holder.btn_delete = (Button) view.findViewById(R.id.btn_delete);
            holder.list_products = (LinearLayout) view.findViewById(R.id.list_products);

            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

        final  PurchaseOrderInfo info = mDataList.get(i);
        holder.tv_order_number.setText(info.getSsn());
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm");
        holder.tv_create_time.setText(format.format(new Date(info.getCreate_at()*1000)));
        holder.tv_should_pay.setText("￥"+info.getShould_pay());
        holder.tv_real_pay.setText("￥"+info.getReal_pay());
        holder.tv_notes.setText("备注：" + info.getSsn_note());

        // 给删除按钮注册回调事件
        holder.btn_delete.setTag(info);
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PurchaseOrderInfo deleteOrder = (PurchaseOrderInfo) view.getTag();
                if (deleteOrder != null){
                    deleteOrderListener.onDelete(deleteOrder);
                }
            }
        });

        // 初始化商品条目
        holder.list_products.removeAllViews();
        List<ProductInfo> productsInfos = info.getProducts();
        if (productsInfos != null) {
            for (int j = 0; j < productsInfos.size(); j++) {
                ProductInfo product = productsInfos.get(j);
                View itemView = LayoutInflater.from(mContext).inflate(R.layout.purchase_order_product_item, null);
                ((TextView) itemView.findViewById(R.id.tv_product_name)).setText(product.getProduct_name());
                ((TextView) itemView.findViewById(R.id.tv_product_num_and_price))
                        .setText("￥" + product.getProduct_price() + "×" + product.getProduct_num());
                ((TextView) itemView.findViewById(R.id.tv_product_total_price)).setText("￥" + product.getProduct_num() * product.getProduct_price());

                holder.list_products.addView(itemView);
            }
        }

        return view;
    }

    private class ViewHolder{
        TextView tv_order_number;
        TextView tv_create_time;
        TextView tv_should_pay;
        TextView tv_real_pay;
        TextView tv_notes;
        Button btn_delete;
        LinearLayout list_products;

    }

    public interface DeleteOrderListener{
        public void onDelete(PurchaseOrderInfo deleteOrder);
    }
}
